package com.example.pclimb.ui.requests;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.example.pclimb.databinding.FragmentRequestsBinding;
import com.example.pclimb.viewmodel.ClimbRequestViewModel;
import com.example.pclimb.viewmodel.UserViewModel;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class RequestsFragment extends Fragment {

    private FragmentRequestsBinding binding;
    private ClimbRequestViewModel climbRequestViewModel;
    private UserViewModel userViewModel;
    private RequestPagerAdapter pagerAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentRequestsBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // 初始化ViewModel
        climbRequestViewModel = new ViewModelProvider(requireActivity()).get(ClimbRequestViewModel.class);
        userViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        
        // 设置ViewPager和TabLayout
        pagerAdapter = new RequestPagerAdapter(this);
        binding.viewPager.setAdapter(pagerAdapter);
        
        // 将TabLayout和ViewPager关联起来
        new TabLayoutMediator(binding.tabLayout, binding.viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("待匹配");
                    break;
                case 1:
                    tab.setText("已匹配");
                    break;
                case 2:
                    tab.setText("待支付");
                    break;
                case 3:
                    tab.setText("已完成");
                    break;
                case 4:
                    tab.setText("已取消");
                    break;
            }
        }).attach();
        
        // 添加陪爬申请按钮点击事件
        binding.fabAddRequest.setOnClickListener(v -> {
            // 跳转到创建陪爬申请界面
            Intent intent = new Intent(requireActivity(), CreateRequestActivity.class);
            startActivity(intent);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        // 当Fragment恢复时刷新数据
        refreshRequests();
    }
    
    /**
     * 刷新陪爬申请数据
     */
    private void refreshRequests() {
        if (climbRequestViewModel != null) {
            // 刷新五个列表的数据
            climbRequestViewModel.refreshPendingRequests();
            climbRequestViewModel.refreshMatchedRequests();
            climbRequestViewModel.refreshAwaitingPaymentRequests();
            climbRequestViewModel.refreshCompletedRequests();
            climbRequestViewModel.refreshCancelledRequests();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
} 